<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" label-width="100px" class="login-form">
      <el-form-item label="用户名" class="form-item">
        <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" class="form-item">
        <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item class="form-item">
        <el-button type="primary" @click="submitForm" class="login-button">登录</el-button>
        <el-button type="default" @click="goToRegister" class="register-button">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
      },
    };
  },
  methods: {
    submitForm() {
  axios.post('/api/user/login', this.loginForm)
    .then(response => {
      if (response.data.success) {
        this.$message.success('登录成功');
        localStorage.setItem('token', response.data.token); // 保存 token
        this.$router.push('/petsinfo'); // 登录成功后重定向
      } else {
        this.$message.error(response.data.message);
      }
    })
    .catch(() => {
      this.$message.error('登录失败');
    });
}
,
    goToRegister() {
      this.$router.push('/adduser');
    },
  },
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #90CAF9 0%, #42A5F5 100%); /* 渐变背景 */
}

.login-form {
  max-width: 400px;
  width: 100%;
  padding: 40px 30px;
  background-color: #ffffffd9; /* 半透明白色背景 */
  border-radius: 15px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}

.form-item {
  margin-bottom: 20px;
}

.el-input {
  width: 100%;
  border-radius: 8px;
}

.login-button {
  background-color: #42A5F5;
  border-color: #42A5F5;
  color: #fff;
  font-size: 1.2em;
  border-radius: 8px;
  transition: background-color 0.3s ease;
}

.login-button:hover {
  background-color: #1E88E5;
  border-color: #1E88E5;
}

.register-button {
  background-color: #90CAF9;
  border-color: #90CAF9;
  color: #fff;
  font-size: 1.2em;
  border-radius: 8px;
  transition: background-color 0.3s ease;
}

.register-button:hover {
  background-color: #64B5F6;
  border-color: #64B5F6;
}

.el-button {
  margin-right: 10px;
  width: 45%;
}
</style>
